package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdSensitiveMapper;
import com.heima.admin.service.AdSensitiveService;
import com.heima.common.exception.CustException;
import com.heima.model.admin.dtos.SensitiveDTO;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.admin.pojos.AdSensitive;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class AdSensitiveServiceImpl extends ServiceImpl<AdSensitiveMapper, AdSensitive> implements AdSensitiveService {



    @Override
    public ResponseResult list(SensitiveDTO dto) {
        dto.checkParam();
        IPage page = new Page(dto.getPage(), dto.getSize());
        LambdaQueryWrapper<AdSensitive> queryWrapper = Wrappers.lambdaQuery();
        if (StringUtils.isNotBlank(dto.getName())) {
            queryWrapper.like(AdSensitive::getSensitives,dto.getName());
        }
        IPage<AdSensitive> pageResult = this.page(page, queryWrapper);

        return new PageResponseResult(dto.getPage() ,dto.getSize(), pageResult.getTotal(),pageResult.getRecords());
    }

    @Override
    public ResponseResult insert(AdSensitive adSensitive) {
        if(StringUtils.isBlank(adSensitive.getSensitives())){
            CustException.cust(AppHttpCodeEnum.PARAM_INVALID,"请输入参数");
        }
        int count=this.count(Wrappers.<AdSensitive>lambdaQuery().eq(AdSensitive::getSensitives,adSensitive.getSensitives()));
        if (count>0){
            CustException.cust(AppHttpCodeEnum.DATA_EXIST);
        }
        adSensitive.setCreatedTime(new Date());
        boolean save = save(adSensitive);
        if (save==true){
            return ResponseResult.okResult();
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"添加失败");
    }

    @Override
    public ResponseResult update(AdSensitive adSensitive) {
        if(StringUtils.isBlank(adSensitive.getSensitives())){
            CustException.cust(AppHttpCodeEnum.PARAM_INVALID,"请输入参数");
        }
        int count=this.count(Wrappers.<AdSensitive>lambdaQuery().eq(AdSensitive::getSensitives,adSensitive.getSensitives()));
        if (count>0){
            CustException.cust(AppHttpCodeEnum.DATA_EXIST);
        }
        boolean b = updateById(adSensitive);
        if (b==true){
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"操作失败");
    }

    @Override
    public ResponseResult delete(Integer id) {
        if (id==null){
            CustException.cust(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        boolean b = removeById(id);
        if (b==true){

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }
        return  ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"删除失败");
    }
@Autowired
AdSensitiveMapper adSensitiveMapper;
    @Override
    public ResponseResult selectAllSensitives() {
        return ResponseResult.okResult(adSensitiveMapper.findAllSensitives());
    }
}
